package com.example.moshudriver.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.BeeFramework.model.BusinessResponse;
import com.sgr.dykdriver.R;
import com.example.moshudriver.activity.SlidingActivity;
import com.example.moshudriver.model.UserModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.tools.MessageConstant;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.tools.SESSION;
import com.example.moshudriver.wxapi.WXEntryActivity;
import com.external.androidquery.callback.AjaxStatus;
import com.external.eventbus.EventBus;
import com.zxing.encoding.EncodingHandler;

//分享推荐 fragment
public class F4_RefferalFragment extends Fragment implements BusinessResponse {
	private ImageView mHomeMenu;
	private Button mShare;
	private UserModel mUserModel;
	private TextView top_view_title,tv_inviteCode;
	private SharedPreferences mShared;
	private ImageView mInviteCode;
	private String invitecode,text;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mainView = inflater.inflate(R.layout.f4_refferal, null);
		mainView.setOnClickListener(null);
		mHomeMenu = (ImageView) mainView.findViewById(R.id.top_view_back);
		mShare = (Button) mainView.findViewById(R.id.btn_share);
		top_view_title = (TextView) mainView.findViewById(R.id.top_view_title);
		top_view_title.setText(R.string.invite_firend);
		mInviteCode = (ImageView) mainView.findViewById(R.id.iv_inviteCode);
		tv_inviteCode = (TextView) mainView.findViewById(R.id.tv_invite_code);
//		f4_text= (TextView) mainView.findViewById(R.id.f4_text);
		mUserModel = new UserModel(getActivity());
		mUserModel.addResponseListener(this);
		mShared = getActivity().getSharedPreferences(
				O2OMobileAppConst.USERINFO, 0);
		invitecode = mShared.getString("invitecode_"
				+ SESSION.getInstance().uid, "");
		text = mShared.getString("text"
				+ SESSION.getInstance().uid, "");
		if ("".equals(invitecode)||"".equals(text)) {
			mUserModel.getInviteCode();
		} else {
		    tv_inviteCode.setText(invitecode);
//		    f4_text.setText(text);
			createQRCode(invitecode);

		}
		if (!EventBus.getDefault().isregister(this)) {
			EventBus.getDefault().register(this);
		}

		return mainView;

	}
	Bitmap qrCodeBitmap;
	// 生成二维码
	private void createQRCode(String invitecode) {
		// TODO Auto-generated method stub
		if (invitecode != null && invitecode.trim().length() > 0) {
			try {
				// 根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（350*350）
				 qrCodeBitmap = EncodingHandler.createQRCode(invitecode,
						350);
				mInviteCode.setImageBitmap(qrCodeBitmap);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		if (EventBus.getDefault().isregister(this)) {
			EventBus.getDefault().unregister(this);
		}
		super.onDestroyView();
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ApiInterface.USER_INVITE_CODE)) {
			createQRCode(mUserModel.invite_code);
			 tv_inviteCode.setText(mUserModel.invite_code);
//			 f4_text.setText(mUserModel.text);
		}

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		mHomeMenu.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((SlidingActivity) getActivity()).showLeft();
			}
		});
		mShare.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), WXEntryActivity.class);
				intent.putExtra(WXEntryActivity.IS_COMMENT, false);
				intent.putExtra(WXEntryActivity.INVITECODE,
						tv_inviteCode.getText());
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			}
		});
	}

	public void onEvent(Object event) {
		Message message = (Message) event;
		if (message.what == MessageConstant.SIGN_IN_SUCCESS) {
			mUserModel.getInviteCode();
		}
	}
}
